const {baseModelUseMysql2} = require("../../js/service/baseModelUseMysql2.js");
const config = require("../../js/config.js");
const mysql = require('mysql2/promise');
const {mysql2Service, useTransaction, pool} = require("../../js/service/mysql2Service.js");
const {baseModel} = require("../../js/service/baseModel");


module.exports = {
    // http://localhost:9023/manage/example03UseMysql2/getAll
    getAll: async (request, response) => {
        let sql = 'select id,name,price from mo_goods where id>75 order by id desc';
        let res = await baseModelUseMysql2.getAll(sql)
        return response.showResult(res)
    },

    // http://localhost:9023/manage/example03UseMysql2/getDataById?id=70
    getDataById: async (request, response) => {
        let {id} = request.query;
        let sql = 'select id, name, price from mo_goods';
        let res = await baseModelUseMysql2.getDataById(sql, id);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/example03UseMysql2/add
    add: async (request, response) => {
        let res = await baseModel.add('mo_goods', {"name": "商品名称", "price": 111});
        return response.showResult(res)
    },

    // http://localhost:9023/manage/example03UseMysql2/updateById
    updateById: async (request, response) => {
        let params = {"id": 138, "name": "商品名称138", "price": 111}
        let res = await baseModel.updateById('mo_goods', params);
        return response.showResult(res)
    },

    // http://localhost:9023/manage/example03UseMysql2/testNonuserTransaction
    testNonuserTransaction: async (request, response) => {
        await baseModel.add('mo_goods', {"name": "商品名称", "price": 111});
        let params = {"id": 138, "name": "商品名称138138138", "price": 111}
        await baseModel.updateById('mo_goodss', params);
        return response.showResult({data: "okkk"})
    },

    // http://localhost:9023/manage/example03UseMysql2/testUseTransaction
    testUseTransaction: async (request, response) => {
        const connection = await pool.getConnection();
        await connection.beginTransaction();
        try {
            await connection.commit();
            let params1 = {"name": "商品名称", "price": 111}
            await baseModel.add('mo_goods', params1, pool);
            let params2 = {"id": 138, "name": "商品名称111", "price": 111}
            await baseModel.updateById('mo_goodss', params2, pool);
            console.log('try')
        } catch (e) {
            await connection.rollback();
            throw e;
        } finally {
            console.log('finally')
            connection.release();  //释放连接回连接池
        }
        return response.showResult({data: "121212"})
        // await useTransaction(async () => {
        //     await baseModel.add('mo_goods', {"name": "商品名称", "price": 111});
        //     let params = {"id": 138, "name": "商品名称111", "price": 111}
        //     await baseModel.updateById('mo_goodss', params);
        //     return response.showResult({data: "ok1"})
        // })
    },

}